/*
	Flie:						MainMenuState.cpp
	Course:				SGD 1207
	Author:				Kori Po
	Purpose:			Handle Main Menu
*/

#include "Game.h"

#include "MainMenuState.h"
#include "GameplayState.h"


#include "Layer.h"

// Utilities
#include <cassert>
#include <ctime>

#include "Player.h"
#include "Enemy.h"

#include "ObjectManager.h"
#include "Kori's Factory.h"
#include "TempFont.h"

#include "EventSystem.h"
#include "Event.h"

#include "LoadState.h"

#include "../TinyXML/tinyxml.h"

#include "MessageSystem.h"
#include "Messages\CreatePlayerMessage.h"
#include "Messages\CreateEnemyMessage.h"
#include "Messages\DestroyPlayerMessage.h"
#include "Messages\DestroyEnemyMessage.h"
#include "Messages\CreateAbilityMessage.h"
#include "Messages\DestroyAbilityMessage.h"
#include "Messages\AttackMessage.h"


CGameplayState::CGameplayState(void)
{
	m_3DGuy					= nullptr;
	m_InputGuy			= nullptr;
	m_TextureGuy		= nullptr;
	m_AudioGuy			= nullptr;

	m_Factory				= nullptr;
	m_ObjManager		= nullptr;
	//m_AniManager        = nullptr;
	m_FactoryGuy		= nullptr;

	m_Messenger			= nullptr;

	m_EventHQ				= nullptr;

	m_Player					= nullptr;

	m_nBackGroundID = -1;
	m_nOptionsBoxID = -1;




	m_nSelectedItem = 0;

	CGame* TheGame = CGame::GetInstance();

	for(int x = 0; x < PAUSEMENUITEMS; x++)
	{
		m_MenuSelection[x] = false;

		RECT r = {TheGame->GetScreenWidth()/8,
			(TheGame->GetScreenHeight()/4) + (75 * x), 
			TheGame->GetScreenWidth()/8 + (225*2),
			(TheGame->GetScreenHeight()/4) + (75 * x) + 43 };

		m_MenuRect[x] = r;
	}

	m_bPause = false;

	m_szMenuItem[0] = "RESUME";

	m_szMenuItem[1] = "EXIT";

	CEventSystem::GetInstance()->ClientResignation ("Spawn Enemy", this);
}


CGameplayState::~CGameplayState(void)
{
	/* Do Nothing */
}

// SINGLETON
CGameplayState* CGameplayState::GetInstance( void )
{
	// Static local variable - Stored in GLOBAL memory
	static CGameplayState s_instance;
	return &s_instance;
}

// EnE Functions
void CGameplayState::Enter(void)
{
	// Wrapper Alloc
	m_3DGuy					= CSGD_Direct3D::GetInstance();
	m_InputGuy			= CSGD_DirectInput::GetInstance();
	m_TextureGuy		= CSGD_TextureManager::GetInstance();
	m_AudioGuy			= CSGD_XAudio2::GetInstance();
	m_FactoryGuy		= CSGD_Factory::GetInstance();

	m_ObjManager			= CObjectManager::GetInstance();
	
	//m_pOM					= CObjectManager::GetInstance();

	m_Animation = new CAnimation;

	// Object Wrappers
	m_ObjManager		= CObjectManager::GetInstance();
	m_Factory				= C_Factory::GetInstance();
	// Comm System
	m_EventHQ				= CEventSystem::GetInstance();
	m_Messenger			= CMessageSystem::GetInstance();


	m_Animation->Init();
	// Asset ID Alloc
	m_nMenuWizardID	= m_TextureGuy->LoadTexture(_T("Resource/TempMouseCursor.png"), D3DCOLOR_XRGB(255,255,255));
	m_nBackGroundID	= m_TextureGuy->LoadTexture(_T("Resource/TempFusionBackground.png"), D3DCOLOR_XRGB(255,255,255));
	m_nOptionsBoxID		= m_TextureGuy->LoadTexture(_T("Resource/TempOptionItemBox.png"), D3DCOLOR_XRGB(255,255,255));

	//m_nTankImgID		= m_TextureGuy->LoadTexture( _T("Resource/NSW_YondaimeSprites.png"),D3DCOLOR_XRGB(255,255,255) );

	//m_FactoryGuy->RegisterClassType< CPlayer >( _T("CPlayerOne")	);

//m_Player1 = m_FactoryGuy->CreateObject( _T("CPlayerOne") );
	//m_Player1->SetImageID( m_nTankImgID );

	m_nGUIID					= m_TextureGuy->LoadTexture(_T("Resource/FusionHUD.png"), D3DCOLOR_XRGB(255,255,255));
	m_nMarkID					= m_TextureGuy->LoadTexture(_T("Resource/XMark.png"), D3DCOLOR_XRGB(255,255,255));
	m_nIconItem				= m_TextureGuy->LoadTexture(_T("Resource/Graphics/IronSword.png"), D3DCOLOR_XRGB(255,255,255));
	m_nIconPlayer			= m_TextureGuy->LoadTexture(_T("Resource/Graphics/PlayerIcon.png"), D3DCOLOR_XRGB(255,255,255));


	// Get Font
	m_MenuFont = new CTempFont( );

	m_Messenger->InitMessageSystem( &MessageProc );

	m_bHasLost = false;

	//Adding a layer to the OM
	CLayer* pLayer = new CLayer();
	this->m_ObjManager->AddObject(pLayer);

	////////////////////////////////////////////////////////////////////////////////
	// Collsion Testing Purposes (no this not how we will make game objects)
	////////////////////////////////////////////////////////////////////////////////
	
	CGame* TheGame = CGame::GetInstance();

	m_Factory->RegisterClassType<CEntity		> ( _T("CEntity"	) );
	m_Factory->RegisterClassType<CPlayer	> ( _T("CPlayer"	) );
	m_Factory->RegisterClassType<CEnemy	> ( _T("CEnemy"	) );
	m_Factory->RegisterClassType<CAbility	> ( _T("CAbility"	) );

	if(m_nCurrentLevel != 1 && m_nCurrentLevel != 2 )
	{
		m_nCurrentLevel = 0;
	}
	if(m_nCurrentWorld != 1 && m_nCurrentWorld != 2 )
	{
		m_nCurrentWorld = 0;
	}

	m_bPause = false;

	CLoadState* TheLoad = CLoadState::GetInstance();

	int* X = TheLoad->GetThis();

	if(*X == 1 || *X == 2 || *X == 3)
	{
		CPlayer*	SomeGuy = TheLoad->GetAGuy();

		CAbility* SomePower = new CAbility;

		SomeGuy->SetAbilityA(SomePower);


	// Set player
	//m_Player1->SetX(TheGame->GetScreenWidth() - (TheGame->GetScreenWidth()/2) );
	//m_Player1->SetY(TheGame->GetScreenWidth()/2);
	//m_Player1->SetHeight(128);
	//m_Player1->SetWidth(128);


	

	

	m_Player->SetType(OBJ_PLAYER);
	m_Enemy->SetType(OBJ_ENEMY);
	//m_Player1->SetType(OBJ_PLAYER);

	//m_ObjManager->AddObject( m_Player1 );
	m_ObjManager->AddObject(m_Player);
	m_ObjManager->AddObject(m_Enemy);

		m_Player = SomeGuy;

		m_ObjManager->AddObject(SomeGuy);
	}
	
	//m_ObjManager->AddObject(m_pAGuy);
	//
	//CAbility* SomePower = new CAbility;
	//
	//ThePlay->m_Player = m_pAGuy;

}

void CGameplayState::Exit(void)
{
	m_bHasLost = false;

	if(m_nMenuWizardID !=1)
	{
		m_TextureGuy->UnloadTexture(m_nMenuWizardID);
		m_nMenuWizardID = -1;
	}
	if(m_nOptionsBoxID !=1)
	{
		m_TextureGuy->UnloadTexture(m_nOptionsBoxID);
		m_nOptionsBoxID = -1;
	}

	// Shutdown Message System
	if( m_Messenger != nullptr )
	{
		m_Messenger->ShutdownMessageSystem();
		m_Messenger = nullptr;
	}

	if( m_EventHQ != nullptr )
	{
		m_EventHQ->ClearEvents();
		m_EventHQ->ShutdownCEventSystem();
		m_EventHQ = nullptr;
	}

	m_3DGuy					= nullptr;
	m_InputGuy			= nullptr;
	m_TextureGuy		= nullptr;
	m_AudioGuy			= nullptr;
}
	
// Running Functions
bool CGameplayState::Input(void)
{
	if( m_InputGuy->KeyDown( DIK_LMENU ) && m_InputGuy->KeyPressed( DIK_RETURN )|| m_InputGuy->KeyDown( DIK_RMENU ) && m_InputGuy->KeyPressed( DIK_RETURN ) )
	{
			CGame* TheGame	=	CGame::GetInstance();
			TheGame->SetIsWindowed(!TheGame->GetIsWindowed());
			m_3DGuy->ChangeDisplayParam(TheGame->GetScreenWidth(),TheGame->GetScreenHeight(),TheGame->GetIsWindowed());
			return true;
	}


	if(m_InputGuy->KeyPressed(DIK_Y) || this->m_InputGuy->JoystickButtonPressed(7, 0))
	{

		if( m_Animation->currAnimation < 6)
		{
			m_Animation->currAnimation += 1;
		}
		if(m_Animation->currAnimation == 6)
		{

			m_Animation->currAnimation = 0;
		}
		
		m_Animation->isPlaying = true;
		m_Animation->isLooping = true;
	}

	if(m_InputGuy->KeyPressed(DIK_U) || this->m_InputGuy->JoystickButtonPressed(2, 0))
	{
		m_Animation->currAnimation = 2;
		m_Animation->isLooping = false;
		this->m_Animation->currFrame = 0;
	}

	if( m_InputGuy->KeyPressedEx(DIK_ESCAPE ) ||m_InputGuy->JoystickButtonPressed( 3, 0))
	{
		m_nSelectedItem = 0;
		m_bPause = !m_bPause;
		return true;
	}

	if(m_bPause)
	{
		if( m_InputGuy->KeyPressedEx(DIK_DOWN) || m_InputGuy->JoystickDPadPressed(DIR_DOWN, 0))
		{
			if(m_nSelectedItem + 1 > 1)
			{
				m_nSelectedItem = 0;
			}
			else
			{
				m_nSelectedItem++;
			}
		}

		if( m_InputGuy->KeyPressedEx(DIK_UP) || m_InputGuy->JoystickDPadPressed(DIR_UP, 0))
		{
			if(m_nSelectedItem - 1 < 0)
			{
				m_nSelectedItem = 1;
			}
			else
			{
				m_nSelectedItem--;
			}
		}

		if( m_InputGuy->KeyPressedEx(DIK_RETURN ) || m_InputGuy->MouseButtonPressed(0) ||m_InputGuy->JoystickButtonPressed( 0, 0))
		{
			if(m_nSelectedItem == 0)
			{
				if(!m_bHasLost)
				{
					m_bPause = false;
				}
				return true;
			}
			else if(m_nSelectedItem == 1)
			{
				if(m_bHasLost)
				{
					m_bHasLost = false;
				}
				m_bPause = false;
				CGame* TheGame	=	CGame::GetInstance();
				CMainMenuState* TheMain = CMainMenuState::GetInstance();
				TheGame->ChangeState(TheMain);
				return true;
			}
		}

		for(int x = 0; x < 2; x++)
		{
			m_MenuSelection[x] = false;
		}

		m_MenuSelection[m_nSelectedItem] = true;
	}
	else
	{
		if( m_InputGuy->KeyPressedEx(DIK_1 ))
		{	
			if(!m_Player)
			{
				CCreatePlayerMessage* pMsg_EX1 = new CCreatePlayerMessage( );
				m_Messenger->SendMsg( pMsg_EX1 );
				pMsg_EX1 = nullptr;
			}
		}
		if( m_InputGuy->KeyPressedEx(DIK_2 ))
		{
			if(!m_Enemy)
			{
				CCreateEnemyMessage* pMsg_EX1 = new CCreateEnemyMessage( );
				m_Messenger->SendMsg( pMsg_EX1 );
				pMsg_EX1 = nullptr;
			}
		}
		if( m_InputGuy->KeyPressedEx(DIK_3 ))
		{
			if(m_Player)
			{
				CDestroyPlayerMessage* pMsg_EX1 = new CDestroyPlayerMessage( m_Player );
				m_Messenger->SendMsg( pMsg_EX1 );
				pMsg_EX1 = nullptr;
			}
		}
		if( m_InputGuy->KeyPressedEx(DIK_4 ))
		{
			if(m_Enemy)
			{
				CDestroyEnemyMessage* pMsg_EX1 = new CDestroyEnemyMessage( m_Enemy );
				m_Messenger->SendMsg( pMsg_EX1 );
				pMsg_EX1 = nullptr;
			}
		}
		if( m_InputGuy->KeyPressedEx(DIK_7 ))
		{
			SaveGame(1);
		}
		if( m_InputGuy->KeyPressedEx(DIK_8 ))
		{
			SaveGame(2);
		}
		if( m_InputGuy->KeyPressedEx(DIK_9 ))
		{
			SaveGame(3);
		}
		if( m_InputGuy->KeyPressedEx(DIK_0 ))
		{
			if(m_Player)
			{
				m_Player->SetHP(0);
			}
		}
	}

	return true;
}

void CGameplayState::Update(float fElapsedTime)
{
	Mouseinthebox( );

	m_Animation->Update(fElapsedTime);

	if(m_bPause)
	{
		Mouseinthebox( );
	}
	else
	{
		m_ObjManager->UpdateAllObjects(fElapsedTime);
		m_ObjManager->CheckCollisions();
		
		//Process Events Call
		m_EventHQ->ProcessEvents();

		//Process Message Call
		m_Messenger->ProcessMessages();

		if(m_Player)
		{
			if(m_Player->GetHP() <= 0)
			{
				CDestroyPlayerMessage* pMsg_EX1 = new CDestroyPlayerMessage( m_Player );
				m_Messenger->SendMsg( pMsg_EX1 );
				pMsg_EX1 = nullptr;

				m_bPause = true;
				m_bHasLost = true;
			}
		}

		if(!this->m_Animation->isPlaying)
		{
			m_Animation->currAnimation = 0;
			m_Animation->isPlaying = true;
			m_Animation->isLooping = true;
		}
	}
}

void CGameplayState::Render(void)
{
	CGame* TheGame = CGame::GetInstance();

	//Draw Background
	RECT ToDraw = {0,0,1920,1080};
	m_TextureGuy->Draw(m_nBackGroundID,0,0,1,1,&ToDraw);

	m_3DGuy->GetSprite()->Flush();


	//m_ObjManager->RenderAllObjects();

	m_ObjManager->RenderAllObjects();

	// Draw GUI
	RECT FrameRect = {0,0,320,116};
	//RECT BarRect = {0,132,252,132+14};
	RECT BarRect = {0,132,512,512};


	int FramePointX = 64;
	int FramePointY = 14;

	int ToDrawFrameX = 5;
	int ToDrawFrameY = 5;

	int OffsetY = 5 + 14;

	m_TextureGuy->Draw(m_nGUIID,ToDrawFrameX,ToDrawFrameY,1,1,&FrameRect);
	m_TextureGuy->Draw(m_nGUIID,ToDrawFrameX + 350,ToDrawFrameY,1,1,&FrameRect);

	int ToDrawHPX = FramePointX + ToDrawFrameX;
	int ToDrawHPY = FramePointY + ToDrawFrameY;
	int ToDrawMPX = FramePointX + ToDrawFrameX;
	int ToDrawMPY = FramePointY + ToDrawFrameY + OffsetY;


	m_Animation->Render();


	TCHAR  	buffer[ 100 ];
	char	strHP[100];
	char	strMP[100];

	if(m_Player)
	{
		// Health Bar
		_stprintf_s( buffer, 100, _T("HP: %i / %i"), int(m_Player->GetHP()), 100 );
		wcstombs_s( nullptr, strHP, 100, buffer, _TRUNCATE );
		m_TextureGuy->Draw(m_nGUIID,ToDrawHPX,ToDrawHPY,float((float)m_Player->GetHP()/100.0),1,&BarRect,0,0,0,D3DCOLOR_XRGB(255,0,0));
		m_MenuFont->Print(strHP, 70, 20, .6, D3DCOLOR_XRGB(255,255,255));

		//Mana Bar
		_stprintf_s( buffer, 100, _T("MP: %i / %i"), int(m_Player->GetMP()), 100 );
		wcstombs_s( nullptr, strMP, 100, buffer, _TRUNCATE );
		m_TextureGuy->Draw(m_nGUIID,ToDrawMPX,ToDrawMPY,float((float)m_Player->GetMP()/100.0),1,&BarRect,0,0,0,D3DCOLOR_XRGB(0,0,255));
		m_MenuFont->Print(strMP, 70, 40, .6, D3DCOLOR_XRGB(255,255,255));

		m_TextureGuy->Draw(m_nMarkID,(int)m_Player->GetX(), (int)m_Player->GetY());

		int ToDrawMUGX = 5 +12;
		int ToDrawMUGY = 5 +12;

		m_TextureGuy->Draw(m_nIconPlayer,ToDrawMUGX, ToDrawMUGY);

		if(m_Player->GetMyItem())
		{
			int ToDrawABX = 5 + 257;
			int ToDrawABY = 5 + 52;

			m_TextureGuy->Draw(m_nIconItem,ToDrawABX, ToDrawABY);
		}
	}

	if(m_Enemy)
	{
		// Health Bar
		_stprintf_s( buffer, 100, _T("HP: %i / %i"), int(m_Enemy->GetHP()), 100 );
		wcstombs_s( nullptr, strHP, 100, buffer, _TRUNCATE );
		m_TextureGuy->Draw(m_nGUIID,ToDrawHPX + 350,ToDrawHPY,float((float)m_Enemy->GetHP()/100.0),1,&BarRect,0,0,0,D3DCOLOR_XRGB(255,0,0));
		m_MenuFont->Print(strHP, 70 + 350, 20, .6, D3DCOLOR_XRGB(255,255,255));

		m_TextureGuy->Draw(m_nMarkID,(int)m_Enemy->GetX(), (int)m_Enemy->GetY());
	}

	TCHAR  	bufferA[ 100 ];
	TCHAR  	bufferB[ 100 ];
	
	char	strLevel[100];
	char	strWorld[100];
	
	_stprintf_s( bufferA, 100, _T("LEVEL: %i"), m_nCurrentLevel );
	_stprintf_s( bufferB, 100, _T("WORLD: %i"), m_nCurrentWorld );

	wcstombs_s( nullptr, strLevel, 100, bufferA, _TRUNCATE );
	wcstombs_s( nullptr, strWorld, 100, bufferB, _TRUNCATE );

	m_TextureGuy->Draw(m_nOptionsBoxID,TheGame->GetScreenWidth() - 300, 5, 1,1);

	m_MenuFont->Print(strLevel, TheGame->GetScreenWidth() - 270, 15 , .8, D3DCOLOR_XRGB(255,255,255));
	m_MenuFont->Print(strWorld, TheGame->GetScreenWidth() - 270, 40 , .8, D3DCOLOR_XRGB(255,255,255));


	if(m_bPause)
	{
		for(int x = 0; x < PAUSEMENUITEMS; x++)
		{
			m_TextureGuy->Draw(m_nOptionsBoxID,TheGame->GetScreenWidth()/8,(TheGame->GetScreenHeight()/4) + (75 * x),2,1);
			if(!m_MenuSelection[x])
			{
				m_MenuFont->Print(m_szMenuItem[x], TheGame->GetScreenWidth()/8 + 50,(TheGame->GetScreenHeight()/4) + (75 * x) + 15, 1, D3DCOLOR_XRGB(255,255,255));
			}
			else
			{
				m_MenuFont->Print(m_szMenuItem[x], TheGame->GetScreenWidth()/8 + 50,(TheGame->GetScreenHeight()/4) + (75 * x) + 15, 1.4, D3DCOLOR_XRGB(255,0,0));
			}
		}

	
		
		
		if(m_bHasLost)
		{
			m_TextureGuy->Draw(m_nOptionsBoxID,TheGame->GetScreenWidth()/2,(TheGame->GetScreenHeight()/8),2,1);
			m_MenuFont->Print("YOU LOSE", TheGame->GetScreenWidth()/2 + 50, TheGame->GetScreenHeight()/8 + 20, 1, D3DCOLOR_XRGB(255,255,255));
		}

		// Draw Mouse
		RECT ToDrawMouse = {0,0,38,48};
		if(m_InputGuy->MouseButtonDown(MOUSE_LEFT))
		{
			RECT ToDrawMouse = {39,0,75,48};
		}

		m_TextureGuy->Draw(m_nMenuWizardID,m_InputGuy->MouseGetPosX(),m_InputGuy->MouseGetPosY(),1,1,&ToDrawMouse);
	}
}

void CGameplayState::Mouseinthebox()
{
	int MouseX = m_InputGuy->MouseGetPosX();
	int MouseY = m_InputGuy->MouseGetPosY();
	
	POINT Clicker;
	Clicker.x = m_InputGuy->MouseGetPosX();
	Clicker.y = m_InputGuy->MouseGetPosY();

	CGame* TheGame = CGame::GetInstance();
	
	for(int x = 0; x < PAUSEMENUITEMS; x++)
	{
		if(PtInRect(&m_MenuRect[x], Clicker))
		{
			m_nSelectedItem = x;
		}
	}
}

void CGameplayState::RegisterEvents( )
{

}

void CGameplayState::HandleEvent( CEvent* pEvent )
{
	if(!m_Enemy && pEvent->GetEventID()=="Spawn Enemy")
	{
		CCreateEnemyMessage* pMsg_EX1 = new CCreateEnemyMessage( );
		m_Messenger->SendMsg( pMsg_EX1 );
		pMsg_EX1 = nullptr;
	}
}

void CGameplayState::SaveGame(int nSlotNumber)
{
	TiXmlDocument TheDoc;

	TiXmlDeclaration* pDecl = new TiXmlDeclaration("1.0", "utf-8", " ");

	TheDoc.LinkEndChild(pDecl);

	// Root Node
	TiXmlElement* pRoot = new TiXmlElement("GameSave");

	TheDoc.LinkEndChild(pRoot);

	//Save  Slot Number
	TiXmlElement* pSlot = new TiXmlElement("Slot");
	pSlot->SetAttribute("SlotNumber", nSlotNumber);
	pRoot->LinkEndChild(pSlot);

	// Node
	TiXmlElement* pPlayer = new TiXmlElement("Player");

	if(m_Player)
	{
		pPlayer->SetAttribute("Active", 1);
		pPlayer->SetAttribute("HP",	this->m_Player->GetHP());
		pPlayer->SetAttribute("MP",	this->m_Player->GetMP());
		pPlayer->SetAttribute("HPRegen", this->m_Player->GetHPRegen());
		pPlayer->SetAttribute("MPRegen", this->m_Player->GetMPRegen());
		pPlayer->SetAttribute("Defense", this->m_Player->GetDefense());
		pPlayer->SetAttribute("AtkPower", this->m_Player->GetAtkPower());
		pPlayer->SetAttribute("AtkSpeed", this->m_Player->GetAtkSpeed());
		pPlayer->SetAttribute("AP", this->m_Player->GetAP());
		pPlayer->SetAttribute("CDR", this->m_Player->GetCDR());
		pPlayer->SetAttribute("Movement", this->m_Player->GetMovement());
		if(this->m_Player->GetMyItem())
		{
			pPlayer->SetAttribute("Item", 1);
		}
		else
		{
			pPlayer->SetAttribute("Item", 0);
		}
		pPlayer->SetAttribute("MaxHP", this->m_Player->GetMaxHP());
		pPlayer->SetAttribute("MaxMP", this->m_Player->GetMaxMP());
		pPlayer->SetAttribute("PosX", this->m_Player->GetX());
		pPlayer->SetAttribute("PosY", this->m_Player->GetY());
		pPlayer->SetAttribute("ObjectType", this->m_Player->GetType());
		pPlayer->SetAttribute("Width", this->m_Player->GetWidth());
		pPlayer->SetAttribute("Height", this->m_Player->GetHeight());
	}
	else
	{
		pPlayer->SetAttribute("Active", 0);
	}

	pRoot->LinkEndChild(pPlayer);

	TiXmlElement* pWorld = new TiXmlElement("World");
	pWorld->SetAttribute("CurrentWorld", m_nCurrentWorld);
	pRoot->LinkEndChild(pWorld);

	TiXmlElement* pLevel = new TiXmlElement("Level");
	pLevel->SetAttribute("CurrentLevel", m_nCurrentLevel);
	pRoot->LinkEndChild(pLevel);

	//Save the File
	
	switch( nSlotNumber )
	{
		case 1:
			TheDoc.SaveFile("Resource/Files/SaveFileA.xml");
			break;

		case 2:
			TheDoc.SaveFile("Resource/Files/SaveFileB.xml");
			break;

		case 3:
			TheDoc.SaveFile("Resource/Files/SaveFileC.xml");
			break;
	}
}

void CGameplayState::MessageProc( IMessage* pMsg )
{
	CGame* TheGame = CGame::GetInstance();
	CGameplayState* self = CGameplayState::GetInstance();

	switch( pMsg->GetMessageID() )
	{
		case MSG_CREATE_PLAYER:
		{
			if(!(self->m_Player))
			{
				CPlayer* Temp;

				Temp = dynamic_cast< CPlayer* >(self->m_Factory->CreateObject( _T("CPlayer") ));
				
				Temp->SetWidth(128);
				Temp->SetHeight(128);
				
				Temp->SetX(float(TheGame->GetScreenWidth()/4));
				 
				Temp->SetY(float(TheGame->GetScreenHeight()/2));

				Temp->SetType(OBJ_PLAYER);

				CAbility* SomePower = new CAbility;
				CEntity* SomeItem = new CEntity;

				Temp->SetAbilityA(SomePower);

				self->m_ObjManager->AddObject(Temp);

				self->m_Player = Temp;
			}
		}
		break;

		case MSG_CREATE_ENEMY:
		{
			if(!(self->m_Enemy))
			{
				CEnemy* Temp;

				Temp	= dynamic_cast< CEnemy* >(self->m_Factory->CreateObject( _T("CEnemy") ));
				//Temp = new CEnemy();
				
				// Set Enemy
				Temp->SetX((float)TheGame->GetScreenWidth() - float(TheGame->GetScreenWidth()/4) );
				Temp->SetY(float(TheGame->GetScreenWidth()/2));
				
				Temp->SetWidth(128);
				Temp->SetHeight(128);
						
				self->m_ObjManager->AddObject(Temp);

				self->m_Enemy = Temp;
			}
		}
		break;

		case MSG_DESTROY_PLAYER:
		{
			if((self->m_Player))
			{
				CDestroyPlayerMessage* pDestroyMessage = dynamic_cast< CDestroyPlayerMessage* >( pMsg );
				assert( pDestroyMessage != nullptr );

				CPlayer* pPlayer = pDestroyMessage->GetMyTarget();

				self->m_ObjManager->RemoveObject( pPlayer );

				pPlayer->Release();

				self->m_Player = nullptr;
			}
		}
		break;

		case MSG_DESTROY_ENEMY:
		{
			if((self->m_Enemy))
			{
				CDestroyEnemyMessage* pDestroyMessage = dynamic_cast< CDestroyEnemyMessage* >( pMsg );
				assert( pDestroyMessage != nullptr );

				CEnemy* pEnemy = pDestroyMessage->GetMyTarget();

				self->m_ObjManager->RemoveObject( pEnemy );

				pEnemy->Release();

				self->m_Enemy = nullptr;
			}
		}
		break;

		case MSG_CREATE_ABILITY:
		{
			CCreateAbilityMessage* pCreateMessage = dynamic_cast< CCreateAbilityMessage* >( pMsg );

			CPlayer* pFiringEntity = pCreateMessage->GetMyMaster();
			assert( pCreateMessage != nullptr );

			CAbility* pAbility = dynamic_cast< CAbility* >( self->m_Factory->CreateObject( _T("CAbility") ) );
			assert( pAbility != nullptr );

			pAbility->SetType(OBJ_ABILITY);

			pAbility->SetOwner(pFiringEntity);
			pAbility->SetX(pFiringEntity->GetX());
			pAbility->SetY(pFiringEntity->GetY());

			pAbility->SetVelX(100);
			pAbility->SetVelY(0);

			self->m_ObjManager->AddObject(pAbility);
			pAbility->Release();
		}
		break;

		case MSG_DESTROY_ABILITY:
		{
			CDestroyAbilityMessage* pDestroyMessage = dynamic_cast< CDestroyAbilityMessage* >( pMsg );
			assert( pDestroyMessage != nullptr );

			CAbility* pAbility = pDestroyMessage->GetMyAbility();

			self->m_ObjManager->RemoveObject( pAbility );

			//pAbility->Release();
		}
		break;

		case MSG_PLAYER_ATTACK:
		{
			if(self->m_Enemy)
			{
				CAttackMessage* pCreateMessage = dynamic_cast< CAttackMessage* >( pMsg );

				CPlayer* pAttacker = pCreateMessage->GetMyMaster();
				assert( pCreateMessage != nullptr );

				RECT rOverlap;

				RECT ToHit = self->m_Enemy->GetRect();

				/*
				RECT AttackRect = {pAttacker->GetX( ) + pAttacker->GetWidth(),
														pAttacker->GetY( ) + pAttacker->GetHeight(),
														pAttacker->GetX( ) +  pAttacker->GetWidth( ) + 100,
														pAttacker->GetY( ) + pAttacker->GetHeight() + 100};
														*/
				RECT AttackRect = {pAttacker->GetX( ),
														pAttacker->GetY( ),
														pAttacker->GetX( ) + 200,
														pAttacker->GetY( ) + 200};

				bool bColliding = IntersectRect( &rOverlap, &AttackRect, &ToHit );

				if(bColliding)
				{
					self->m_Enemy->SetHP(self->m_Enemy->GetHP() - 20);
				}
			}
		
		}
		break;
	}
}

